import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../../models/adress_info_model.dart';

class ConfirmAddressView extends StatefulWidget {
  final List<AdressInfo> adressInfo;
  final VoidCallback onCLickAdd;
  final ValueChanged<int> onChangeAddress;
  const ConfirmAddressView({super.key, required this.adressInfo, required this.onCLickAdd, required this.onChangeAddress});

  @override
  _ConfirmAddressViewState createState() => _ConfirmAddressViewState();
}

class _ConfirmAddressViewState extends State<ConfirmAddressView> {
  // 定义选中的地址索引，默认为0
  int selectIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 15, right: 15, top: 15),
      child: Container(
        decoration: BoxDecoration(
          color: const Color(0xFFEAEAFF),
          borderRadius: BorderRadius.circular(15),
        ),
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(left: 15,top: 10,bottom: 10),
              child: Row(
                children: [
                  Container(
                    width: 18,
                    height: 18,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: const Color(0xFF5D5FEE),
                      borderRadius: BorderRadius.circular(9),
                    ),
                    child: const Text(
                      '4',
                      style: TextStyle(color: Colors.white, fontSize: 13),
                    ),
                  ),
                  const SizedBox(width: 7),
                  Text(
                    AppLocalizations.of(context)!.zhangdandizhi,
                    style: const TextStyle(fontSize: 14, color: Colors.black),
                  ),
                  const Spacer(),
                  if (widget.adressInfo.isNotEmpty)            
                  TextButton.icon(
                    icon: SizedBox(
                      width: 20,
                      height: 20,
                      child: Image.asset('assets/images/下载.png'),
                    ),
                    label: Text(AppLocalizations.of(context)!.newcreateAddress),
                    style: TextButton.styleFrom(
                      foregroundColor: const Color(0xFF5D5FEE),
                      textStyle: const TextStyle(fontSize: 15),
                      padding: const EdgeInsets.only(right: 15,top: 0,bottom: 0),
                      minimumSize: Size.zero,
                      tapTargetSize: MaterialTapTargetSize.shrinkWrap
                    ),
                    onPressed: () {
                      widget.onCLickAdd();
                    },
                  ),
                ],
              ),
            ),
            // 使用 shrinkWrap 来使 ListView 根据内容自适应高度
    ClipRRect(
  borderRadius: BorderRadius.circular(15),
  child: Container(
    color: Colors.white, // 设置背景色为白色
    child: widget.adressInfo.isEmpty
        ? Center(
            child: Container(
              color: Colors.white,
              child: TextButton.icon(
                icon: SizedBox(
                  width: 28,
                  height: 28,
                  child: Image.asset('assets/images/下载.png'),
                ),
                label: Text(AppLocalizations.of(context)!.newcreateAddress),
                style: TextButton.styleFrom(
                  foregroundColor: const Color(0xFF5D5FEE),
                  textStyle: const TextStyle(fontSize: 17),
                  padding: const EdgeInsets.only(right: 17),
                ),
                onPressed: () {
                  widget.onCLickAdd();
                },
              ),
            ),
          )
        : ListView.builder(
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: widget.adressInfo.length,
            itemBuilder: (context, index) {
              final model = widget.adressInfo[index];
              return Column(
                children: [
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        selectIndex = index;
                      });
                    },
                    child: AdressCell(
                      isSelected: selectIndex == index,
                      model: model,
                      onSetBtnTapped: () {
                        widget.onChangeAddress(index);
                      },
                    ),
                  ),
                  if (index != widget.adressInfo.length - 1)
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 15),
                      child: Container(
                        height: 1,
                        color: Color(0xFFF6F6F6),
                      ),
                    ),
                ],
              );
            },
          ),
  ),
)

          ],
        ),
      ),
    );
  }
}



class AdressCell extends StatelessWidget {
  final bool isSelected;
  final AdressInfo model;
  final VoidCallback onSetBtnTapped;

  const AdressCell({
    super.key,
    required this.isSelected,
    required this.model,
    required this.onSetBtnTapped,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          color: Colors.white, // highlight selected row
          padding: const EdgeInsets.symmetric(vertical: 17, horizontal: 15),
          child: Row(
            children: [
              Image.asset(
                isSelected ? 'assets/images/address1.png' : 'assets/images/address0.png',
                width: 18,
                height: 18,
              ),
              const SizedBox(width: 10),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    width: MediaQuery.of(context).size.width * 2 / 3,
                    child: Text(
                      '${model.firstName} ${model.lastName} ${model.phoneNumber}',
                      style: TextStyle(
                        fontSize: 14,
                        color: isSelected ? Colors.blue : Color(0xFF333333),
                      ),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                    ),
                  ),
                ],
              ),
              Spacer(),
              GestureDetector(
                onTap: onSetBtnTapped,
                child: Image.asset(
                  'assets/images/setbtn.png',
                  width: 18,
                  height: 18,
                ),
              ),
            ],
          ),
        ),
   
      ],
    );
  }
}
